<template>
  <div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline" size="small">
      <el-form-item label="关键字">
        <el-input v-model="formInline.keyword" placeholder="请输入姓名/邮箱"></el-input>
      </el-form-item>
      <el-form-item label="部门">
        <el-select v-model="formInline.deptId" placeholder="选择部门">
          <el-option v-for="dep in departments" :key="dep.id" :label="dep.name" :value="dep.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
        <el-button type="success" @click="add">添加</el-button>
        <el-button type="danger" @click="delmore">批量删除</el-button>
        <!-- <el-button type="warning" @click="onSubmit">导入</el-button> -->
        <el-button type="warning" @click="exportFile">导出</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableData" border style="width: 100%" @selection-change="selectChange">
      <el-table-column type="selection"></el-table-column>
      <el-table-column prop="id" label="编号"></el-table-column>
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="age" label="年龄"></el-table-column>
      <el-table-column prop="email" label="email"></el-table-column>
      <el-table-column prop="dept.name" label="部门"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="editEmployee(scope.row)">编辑</el-button>
          <el-button type="danger" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
    <my-table :data="tableData">
      <button slot-scope="scope" @click="myEdit(scope)">编辑</button>
    </my-table>
    <!-- <my-table :data="tableData">
      <button>编辑</button>
      <button>删除</button>
    </my-table> -->
  </div>
</template>

<script>
import { getDepartments, getEmployees, delEmployees, exportFileUrl } from "@/request/httpApi";
import MyTable from './MyTable'
export default {
  components: {
    MyTable
  },
  data() {
    return {
      formInline: {},
      departments: [],
      tableData: [],
      currentPage: 1, // 当前页
      pageSize: 10, // 显示数据条数
      total: 0,
      selarr: []
    };
  },
  methods: {
    exportFile(){
      //js创建a标签
      const aDom = document.createElement("a");
      aDom.setAttribute(
        "href",
        exportFileUrl + "?token=" + localStorage.getItem("token")
      );
      aDom.click();
    },
    myEdit(scope) {
      console.log(scope)
    },
    // 点击编辑后触发的函数
    editEmployee(row) {
      console.log(row);
      this.$router.push('/employee/edit/' + row.id)
    },
    // 当选项改变的时候会触发（拿到当前选中的值）
    selectChange(selection) {
      // console.log(selection)
      this.selarr = selection.map(item => item.id);
      // console.log(selarr)
    },
    // 点击批量删除触发的方法
    delmore() {
      console.log(this.selarr);
      if (this.selarr.length == 0) return;
      this.$confirm("此操作将永久删除员工, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          delEmployees({ids: this.selarr}).then(res => {
          })
        })
        .catch(() => {
        });
    },
    add() {
      this.$router.push("/employee/add");
    },
    search() {
      this.fetchData();
    },
    fetchData() {
      getEmployees({
        currentPage: this.currentPage,
        keyword: this.formInline.keyword,
        deptId: this.formInline.deptId,
        pageSize: this.pageSize
      }).then(res => {
        if (res.success) {
          console.table(res.data.list);
          this.tableData = res.data.list;
          // 设置分页总条数
          this.total = res.data.total;
        }
      });
    },
    // 每一页显示的数量发生改变的时候触发
    handleSizeChange(size) {
      console.log(size);
      this.pageSize = size;
      // 重新发送请求
      this.fetchData();
    },
    handleCurrentChange(page) {
      console.log(page);
      this.currentPage = page;
      this.fetchData();
    },

    onSubmit() {}
  },
  created() {
    // 发送请求获取部门列表
    getDepartments({
      currentPage: 1,
      pageSize: 100
    }).then(res => {
      // console.table(res.data.list);
      this.departments = res.data.list;
    });

    // 发送请求获取员工列表
    this.fetchData();
  }
};
</script>

<style>
</style>